package com.bluetooth.phone.multi;

import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder {
    public SparseArray<View> mViews;
    public View mItemView;

    public BaseViewHolder(View itemView) {
        super(itemView);
        mItemView = itemView;
        mViews = new SparseArray<>();
    }

    public View getView(int resId) {
        View view = mViews.get(resId);

        if (view == null) {
            view = mItemView.findViewById(resId);
            mViews.put(resId, view);
        }
        return view;
    }
    public Object itemData;

    public Object getItemData() {
        return itemData;
    }
    /**
     * 设置文本
     */
    public void setText(int resId, String content) {
        View view = mViews.get(resId);
        if (view == null) {
            view = mItemView.findViewById(resId);
            mViews.put(resId, view);
        }
        if (view instanceof TextView) {
            TextView textView = (TextView) view;
            textView.setText(content);
        }
    }

    /**
     * 设置图片
     */
    public void setImage(int id, int resId) {
        View view = mViews.get(resId);
        if (view == null) {
            view = mItemView.findViewById(resId);
            mViews.put(resId, view);
        }
        if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            imageView.setImageResource(resId);
        }

    }


    public abstract void bindViewData(T data);
}